<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<script type="text/javascript" src="js/jquery.itextclear.js"></script>
<script type="text/javascript">
    $(document).ready(function() {
        $('input[type=text], input[type=password], input[type=url], input[type=email], input[type=number], textarea', '.form').iTextClear();
    });
</script>
<!-- jQplot CSS -->

<link rel="stylesheet" media="screen" href="CSS/Tree.css"/>
<!-- jQplot CSS END -->

<!-- DATATABLES CSS -->
<link rel="stylesheet" media="screen" href="lib/datatables/css/vpad.css"/>
<script type="text/javascript" src="lib/datatables/js/jquery.dataTables.js"></script>

<!-- DATATABLES CSS END -->
<h1 class="page-title">考核项目设定 - ${listTitle}</h1>

<div class="container_12 clearfix leading">
    <div class="side grid_12 leading">
        <form class="form has-validation" id="post_form" name="post_form">
            <div class="clear"></div>
            <div class="clearfix">
                <label for="title" class="form-label">考核项目 <em>*</em></label>
                <div class="form-input">
                    <input type="text" id="title" name="examQualItemSet.title" required="required" placeholder="请输入考核项目" value="${examQualItemSet.title}"/>
                </div>
            </div>
            <div class="clearfix">
                <label for="percentage" class="form-label">占比 <em>*</em></label>
                <div class="form-input">
                    <input type="text" id="percentage" name="examQualItemSet.percentage" required="required" placeholder="请输入0-${percentage}的百分比" value="${examQualItemSet.percentage}" onblur="check_percentage()"/>
                </div>
            </div>

            <s:if test="#request.examQualStandardSets!=null and #request.examQualStandardSets.size() > 0">
            <s:iterator value="#request.examQualStandardSets" var="qss" status="sta">
            <div class="clearfix">
                <label for="desc${sta.index+1}" class="form-label">${qss.name}(${qss.scoreStart}-${qss.scoreEnd}) <em>*</em></label>
                <div class="form-input">
                    <textarea placeholder="请输入此分数区间的说明" rows="2" id="desc${sta.index+1}" name="examQualItemSet.desc${sta.index+1}" t_type="QualStandardSet" t_name="${qss.name}"></textarea>
                </div>
            </div>
            </s:iterator>
            </s:if>

            <div class="form-action clearfix">
                <s:if test="#request.examQualStandardSets!=null and #request.examQualStandardSets.size() > 0">
                <a href="javascript:void(0)" onclick="checkForm()" class="button">${examQualItemSet != null && examQualItemSet.id != null && examQualItemSet.id > 0 ? "修改":"添加"}</a>
                </s:if>
                <%--<button class="button" type="submit" onclick="checkForm()">添加</button>--%>
                <button class="button" type="reset">重置</button>
                <a class="button" href="#exam/qual_item_main.xhtml?organizationId=${organizationId}">
                    <span style="background:url(${ctx}/images/img/arrow_undo.png)" class="icon">&nbsp;</span>返回
                </a>
            </div>

            <input type="hidden" name="examQualItemSet.id" value="${examQualItemSet.id}">
            <input type="hidden" name="examQualItemSet.organization.id" value="${organizationId}">
            <input id="has_items" type="hidden" value='<s:property value="#request.examQualStandardSets!=null and #request.examQualStandardSets.size() > 0"/>'>
        </form>
    </div>
</div>

<div class="clear"></div>


<script type="text/javascript" src="${ctx}/js/global_util.js"></script>
<script type="text/javascript">
    function check_percentage(){
        var p = $("#percentage").val();
        if(checkNull(p)){
            alert("请填写百分比");
            return false;
        }
        if(isNaN(p)){
            alert("请正确填写百分比");
            $("#percentage").attr("value", "");
            return false;
        }
        var sp = ${percentage};
        if(p > sp || p < 0){
            alert("请输入0~"+sp+"之间的数")
            $("#percentage").attr("value", "");
            return false;
        }
        return true;
    }

    function checkForm(){
        if(checkNull($("#title").val())){
            alert("请正确输入考核项目名")
            return;
        }
        
        if(!check_percentage()){
            return;
        }

        var flag = false;
        $("textarea").each(function() {
            if ($(this).attr("t_type") == "QualStandardSet") {
                if(checkNull($(this).val())){
                    alert("请输入"+$(this).attr("t_name")+"的说明");
                    flag = true;
                    return false;
                }
            }
        });
        if(flag){
            return;
        }

        var url = "${ctx}/exam/qual_item_create.xhtml";
        $.ajax(url, {
            type:"POST",
            data:$("#post_form").serialize(),
            dataType:"html",
            cache:false,
            success:function(a) {
                return pageDownloaded(a, url)
            },
            complete:function(b) {
            }
        });
    }

    $(function(){
        if(!$("#has_items")){
            alert("没有考核标准数据,请先设置考核标准");
            //window.location.href="#exam/qual_item_main.xhtml"
        }else{
            var eid = "${examQualItemSet.id}";
            //修改
            if(eid > 0){
                var descs = new Array();
                descs.push("${examQualItemSet.desc1}");
                descs.push("${examQualItemSet.desc2}");
                descs.push("${examQualItemSet.desc3}");
                descs.push("${examQualItemSet.desc4}");
                descs.push("${examQualItemSet.desc5}");
                descs.push("${examQualItemSet.desc6}");
                descs.push("${examQualItemSet.desc7}");
                descs.push("${examQualItemSet.desc8}");
                descs.push("${examQualItemSet.desc9}");
                descs.push("${examQualItemSet.desc10}");
                //初始化相应字段
                var index = 0;
                $("textarea").each(function() {
                    if ($(this).attr("t_type") == "QualStandardSet") {
                        $(this).val(descs[index]);
                        index++;
                    }
                });
            }
        }
    });
</script>